package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.PopularProgramEntity;
import tools.ItemOnClickListenerOther;
import utils.roundimage.RoundNetWorkImageView;
import tools.TimeUtil;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/19.
 */
public class PopularProgramAdapter extends
        RecyclerView.Adapter<PopularProgramAdapter.PopularProgramViewHolder> {
    private ImageLoader imageLoader;
    private List<PopularProgramEntity.ListEntity> entities;
    private ItemOnClickListenerOther listener;

    public void setItemOnClickListener(ItemOnClickListenerOther listener) {
        this.listener = listener;
    }

    public PopularProgramAdapter(Context context) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addList(List<PopularProgramEntity.ListEntity> entities) {
        this.entities = entities;
        notifyDataSetChanged();
    }

    @Override
    public PopularProgramViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.fragment_program_popular_item, parent, false);
        return new PopularProgramViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PopularProgramViewHolder holder, int position) {
        if (isListNull()) {
            PopularProgramEntity.ListEntity entity = entities.get(position);
            holder.rankingTv.setText(position + 1 + "");
            if (position == 0)
                holder.rankingTv.setTextColor(0xFFF14E2F);
            if (position == 1)
                holder.rankingTv.setTextColor(0xFFF28600);
            if (position == 2)
                holder.rankingTv.setTextColor(0xFF8FB000);
            if (position != 0 && position != 1 && position != 2)
                holder.rankingTv.setTextColor(0xFFcdcdcd);

            holder.popularProgramImg.setImageUrl(entity.getCoverSmall(), imageLoader);
            holder.popularProgramTitle.setText(entity.getTitle());
            holder.popularProgramContent.setText("by " + entity.getNickname());
            holder.popularProgramTime.setText(TimeUtil.getDescriptionTimeFromTimestamp(entity.getUpdatedAt()));
        }

        holder.pos = position;
    }

    private boolean isListNull() {
        return entities != null && entities.size() > 0;
    }

    @Override
    public int getItemCount() {
        return isListNull() ? entities.size() : 0;
    }

    public class PopularProgramViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private RoundNetWorkImageView popularProgramImg;
        private TextView rankingTv, popularProgramTitle, popularProgramContent, popularProgramTime;
        private ImageView popularProgramDowload;
        private int pos;
        private LinearLayout popularProgramInfor;

        public PopularProgramViewHolder(View itemView) {
            super(itemView);
            popularProgramImg = (RoundNetWorkImageView) itemView.findViewById(R.id.popularProgramImg);
            popularProgramImg.setOnClickListener(this);
            popularProgramInfor = (LinearLayout) itemView.findViewById(R.id.popularProgramInfor);
            rankingTv = (TextView) itemView.findViewById(R.id.rankingTv);
            popularProgramTitle = (TextView) itemView.findViewById(R.id.popularProgramTitle);
            popularProgramContent = (TextView) itemView.findViewById(R.id.popularProgramContent);
            popularProgramTime = (TextView) itemView.findViewById(R.id.popularProgramTime);
            popularProgramDowload = (ImageView) itemView.findViewById(R.id.popularProgramDowload);
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (entities != null && entities.size() > 0) {
//                        PopularProgramEntity.ListEntity entity = entities.get(pos);
//                        if (listener != null) {
//                                listener.itemListener(entity, 0);
//                        }
//                    }
//                }
//            });
            popularProgramDowload.setOnClickListener(this);
            popularProgramInfor.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (entities != null && entities.size() > 0) {
                PopularProgramEntity.ListEntity entity = entities.get(pos);
                if (listener != null) {
                    switch (v.getId()) {
                        case R.id.popularProgramDowload:
                            listener.itemListener(entity, 1);
                            break;
                        case R.id.popularProgramInfor:
                            listener.itemListener(entity, 0);
                            break;
                    }
                }
            }

        }
    }
}
